package oocl.eshop.ui.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.apache.tomcat.util.http.fileupload.servlet.ServletRequestContext;

import oocl.eshop.po.Merchant;
import oocl.eshop.service.MerchantManager;
import oocl.eshop.service.impl.MerchantManagerImpl;
import oocl.eshop.util.jms.JmsSendXML;

/**
 * Servlet implementation class RegisterServlet
 */
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	MerchantManager merchantManager = new MerchantManagerImpl();

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// //fileupload的参数设置对象
		// DiskFileItemFactory factory = new DiskFileItemFactory();
		// factory.setSizeThreshold(1024*300);
		// //fileupload的工作核心对象
		// ServletFileUpload fileUpload = new ServletFileUpload(factory);
		// //fileItem封装各个表单项，每个fileitem是一个input
		// try {
		// List<FileItem> fileItems = fileUpload.parseRequest(new
		// ServletRequestContext(request));
		// Merchant merchant=merchantManager.LoadFileMerchant(fileItems);
		// Boolean addSuccess = JmsSendXML.jmsXML(merchant);
		// if (addSuccess) {
		// response.sendRedirect("login.jsp");
		// } else {
		// request.getRequestDispatcher("register.jsp");
		// }
		// } catch (FileUploadException e) {
		//
		// e.printStackTrace();
		// }
		Merchant merchant = (Merchant) request.getAttribute("thisMerchant");
		Boolean addSuccess = JmsSendXML.jmsXML(merchant);
		if (addSuccess) {
			System.out.println("success");
			response.sendRedirect("login.jsp");
		} else {
			System.out.println("Error");
			request.getRequestDispatcher("register.jsp");
		}

		// Merchant merchant = new Merchant(mPersonName, mTel, mPassword,
		// mIdCard, mCardPath, mLocation, mBrand, mLogoPath);
		// Boolean addSuccess = JmsSendXML.jmsXML(merchant);
		// if(addSuccess){
		// response.sendRedirect("login.jsp");
		// }else{
		// request.getRequestDispatcher("register.jsp");
		// }

	}

}
